package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.pb.union.Parergon;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionParergonMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_UNION_PARERGON_BUY_PRODUCT, desc = "购买")
public class CrossBuyCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionProto.ParergonBuyCrossReqMsg reqMsg = CrossUnionProto.ParergonBuyCrossReqMsg.parseFrom(packet.getBytes());
        int bussinessId = reqMsg.getBussinessId();
        long price = reqMsg.getPrice();
        int productId = reqMsg.getProductId();

        CrossUnionProto.ParergonBuyCrossRespMsg.Builder respMsg = CrossUnionParergonMgr.buy(packet.getPlayerId(), bussinessId, productId, price);

        if(respMsg.getRet() == 0){
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_UNION_PARERGON_BUY_PRODUCT_FROM_CROSS, respMsg));
        }else {
            Parergon.BuyProductRespMsg.Builder msg = Parergon.BuyProductRespMsg.newBuilder();
            msg.setRet(respMsg.getRet());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_PARERGON_BUY_PRODUCT, msg));
        }
    }

    @Override
    public void sendData() {

    }
}
